<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/2/13/013
 * Time: 14:09
 */

namespace app\dlc\common\form;

//异常工具
use Think\Request;
use Think\Validate;


class ValidateTool extends Validate
{
    public function __construct(array $rules = [], array $message = [], array $field = [])
    {
        $this->rule = $rules;
        parent::__construct($rules, $message, $field);
    }

    //type = 1,前端验证返回  2.后端验证返回
    public function goCheck($type = 1)
    {
        //必须设置contetn-type:application/json
        $request = Request::instance();
        $params = $request->param();
        if (!$this->check($params)) {
            if ($type == 1) {
                api_return(0,$this->error);
            } else {
                admin_return(0,$this->error);
            }
        }
        return true;
    }

    //手机号验证
    protected function isMobile($value)
    {
        $rule = '^1(3|4|5|7|8)[0-9]\d{8}$^';
        $result = preg_match($rule, $value);
        if ($result) {
            return true;
        } else {
            return false;
        }
    }

//    判断是否数组
    protected function isArray($value){
        if (is_array($value)){
            return true;
        }else{
            if (is_array(json_decode($value,true))){
                return true;
            }else{
                return false;
            }
        }
    }

    //结束时间不能小于开始时间
    protected function afterStart($value, $rule, $data)
    {
        return strtotime($data[$rule]) < strtotime($value);
    }
    //正整数
    protected function pInt($value, $rule, $data){
        if(!preg_match("/^[1-9][0-9]*$/",$value)||$value<0){//正整数
            return false;
        }
        return true;

    }
}